﻿@inject IJSRuntime JS

<div class="cv-carousel-card">
    <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaCarousel Model="Images" OnClick="OnClick" AspectRatio="4/3D" />
</div>

<CnGalWebSite.Shared.MasaComponent.Shared.Components.TrackEvent @ref="trackEven" />

@code {
    [Parameter]
    public List<CarouselViewModel> Model { get; set; } = new List<CarouselViewModel>();

    private List<CarouselViewModel> Images = new List<CarouselViewModel>();
    CnGalWebSite.Shared.MasaComponent.Shared.Components.TrackEvent trackEven;

    protected override void OnInitialized()
    {

        Images = Model.OrderByDescending(s => s.Priority).Take(3).ToList();
        if (Model.Count >= 6)
        {
            Images.AddRange(Model.Where(s => Images.Any(x => s.Image == x.Image) == false).ToList().Random().Take(3));

        }
        else
        {
            Images.AddRange(Model.Where(s => Images.Any(x => s.Image == x.Image) == false));
        }

    }


    private async Task OnClick(CarouselViewModel model)
    {
        if (trackEven != null)
        {
            await trackEven.SendTrackEvent(TrackEventType.Click, TrackEventDataType.Carousel, model.Id.ToString(), model.Note);
        }
    }
}
